/**********************************************************************************************************************
 * SplendidCRM is a Customer Relationship Management program created by SplendidCRM Software, Inc. 
 * Copyright (C) 2005-2011 SplendidCRM Software, Inc. All rights reserved.
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU Affero General Public License as published by the Free Software Foundation, either version 3 
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License along with this program. 
 * If not, see <http://www.gnu.org/licenses/>. 
 * 
 * You can contact SplendidCRM Software, Inc. at email address support@splendidcrm.com. 
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3, 
 * the Appropriate Legal Notices must display the following words on all interactive user interfaces: 
 * "Copyright (C) 2005-2011 SplendidCRM Software, Inc. All rights reserved."
 *********************************************************************************************************************/
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Data;
using System.Drawing.Imaging;
using System.Diagnostics;

namespace SplendidCRM.EmailClient.Pop
{
	/// <summary>
	/// Summary description for Attachment.
	/// </summary>
	public partial class Attachment : SplendidPage
	{
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// 02/26/2012 Paul.  We don't want to generate an error during precompile. 
			if ( Request["PrecompileOnly"] == "1" )
				return;
			try
			{
				string sUNIQUE_ID     = Sql.ToString(Request["UNIQUE_ID"    ]);
				string sATTACHMENT_ID = Sql.ToString(Request["ATTACHMENT_ID"]);
				if ( !Sql.IsEmptyString(sUNIQUE_ID) && !Sql.IsEmptyString(sATTACHMENT_ID) )
				{
					string sSERVER_URL         = Sql.ToString (Session["Pop3.SERVER_URL"    ]);
					string sEMAIL_USER         = Sql.ToString (Session["Pop3.EMAIL_USER"    ]);
					string sEMAIL_PASSWORD     = Sql.ToString (Session["Pop3.EMAIL_PASSWORD"]);
					int    nPORT               = Sql.ToInteger(Session["Pop3.PORT"          ]);
					bool   bMAILBOX_SSL        = Sql.ToBoolean(Session["Pop3.MAILBOX_SSL"   ]);
					string sMAILBOX            = Sql.ToString (Session["Pop3.MAILBOX"       ]);
					string sDECRYPTED_PASSWORD = String.Empty;
					
					Guid gINBOUND_EMAIL_KEY = Sql.ToGuid(Application["CONFIG.InboundEmailKey"]);
					Guid gINBOUND_EMAIL_IV  = Sql.ToGuid(Application["CONFIG.InboundEmailIV" ]);
					if ( !Sql.IsEmptyString(sEMAIL_PASSWORD) )
						sDECRYPTED_PASSWORD = Security.DecryptPassword(sEMAIL_PASSWORD, gINBOUND_EMAIL_KEY, gINBOUND_EMAIL_IV);
					
					bool   bIsInline    = false;
					string sFileName    = String.Empty;
					string sContentType = String.Empty;
					int nATTACHMENT_ID = Sql.ToInteger(sATTACHMENT_ID);
					byte[] byDataBinary = PopUtils.GetAttachmentData(Context, sSERVER_URL, nPORT, bMAILBOX_SSL, sEMAIL_USER, sDECRYPTED_PASSWORD, sUNIQUE_ID, nATTACHMENT_ID, ref sFileName, ref sContentType, ref bIsInline);
					if ( byDataBinary != null )
					{
						Response.ContentType = sContentType;
						if ( !bIsInline )
							Response.AddHeader("Content-Disposition", "attachment;filename=" + Utils.ContentDispositionEncode(Request.Browser, sFileName));
						using ( BinaryWriter writer = new BinaryWriter(Response.OutputStream) )
						{
							writer.Write(byDataBinary);
						}
					}
					else
					{
						throw(new Exception("Attachment " + sATTACHMENT_ID + " not found in " + sUNIQUE_ID));
					}
				}
				else
				{
					throw(new Exception("Message or attachment not specified."));
				}
			}
			catch(Exception ex)
			{
				SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex);
				//Response.Write(ex.Message);
				if ( ex.GetType() != Type.GetType("System.Threading.ThreadAbortException") )
				{
					byte[] byImage = Images.Image.RenderAsImage(Response, 300, 100, ex.Message, ImageFormat.Gif);
					Response.ContentType = "image/gif";
					Response.BinaryWrite(byImage);
				}
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
		}
		#endregion
	}
}


